package com.thoughtworks.exercise;

public class Triangle {

    private static final String SYMBOL = "*";
    private static final String LINE_BREAK = "\n";
    private static final String SPACE = " ";

    public void printAsteriskOnlyOnce(){
        printHorizontalLineOfAsterisk(1);
    }

    public void printHorizontalLineOfAsterisk(int amount){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < amount; i++){
            sb.append(SYMBOL);
        }
        printOut(sb.toString());
    }

    public void printVerticalLineOfAsterisk(int amount){
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < amount; i++){
            sb.append(SYMBOL + LINE_BREAK);
        }
        printOut(sb.toString());
    }

    public void printIsoscelesTriangle(int amount){
        StringBuilder sb = new StringBuilder();
        int maxSizeOfTheLine = (amount * 2 ) - 1;
        int middleOfTheLine = (amount / 2) + 1;
        for (int currentLine = 0; currentLine < amount; currentLine++) {
            for (int currentPositionOfLine = 0; currentPositionOfLine < maxSizeOfTheLine; currentPositionOfLine++) {
                if (haveAsterisk(middleOfTheLine,currentLine,currentPositionOfLine)){
                    sb.append(SYMBOL);
                } else {
                    sb.append(SPACE);
                }
            }
            sb.append(LINE_BREAK);
        }
        printOut(sb.toString());
    }

    private boolean haveAsterisk(int middleOfTheLine, int currentLine, int currentPosition){
        int startPosition = middleOfTheLine - currentLine;
        int endPosition = middleOfTheLine + currentLine;
        return ( currentPosition >= startPosition && currentPosition <= endPosition ) ? true : false;
    }

    public void printOut(String out){
        System.out.println(out);
    }

}
